﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.Common;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Areas.Sys.Controllers
{
    public class Sys_EmailTemplateController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult GetPageList(int rowsPerPage, int offsetRows, string sortName, string sortOrder, Sys_EmailTemplate emailTemplate)
        {
            try
            {
                if (rowsPerPage < 1 || offsetRows < 0)
                {
                    return Json(new { total = 0, rows = new List<Sys_EmailTemplate>() });
                }
                (List<Sys_EmailTemplate> roles, int count) = new Sys_EmailTemplateBusiness().GetPageList(offsetRows / rowsPerPage, rowsPerPage, emailTemplate, sortName + " " + sortOrder);
                return Json(new { total = count, rows = roles });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取邮件模板" +
                    "分页信息失败", $"rowsPerPage={rowsPerPage}，offsetRows={offsetRows}，emailTemplate={JsonConvert.SerializeObject(emailTemplate)}，sortName={sortName}，sortOrder={sortOrder}", ex, "DapperCore");
                return Json(new { total = 0, rows = new List<Sys_EmailTemplate>() });
            }
        }

        [HttpPost]
        public IActionResult Add(Sys_EmailTemplate emailTemplate)
        {
            try
            {
                Sys_EmailTemplateBusiness bussiness = new Sys_EmailTemplateBusiness();
                if (bussiness.IsExist(emailTemplate.TemplateCode, emailTemplate.TemplateName))
                {
                    return Json(new { Success = false, Message = "本邮件模板已存在，请修改模板编码和模板名称！" });
                }
                #region 完善Sys_EmailTemplate
                emailTemplate.Id = Guid.NewGuid().ToString();
                emailTemplate.IsDeleted = 0;
                emailTemplate.Creator = SessionUtility.GetBasicUserInfo().UserName;
                emailTemplate.CreateDate = DateTime.Now;
                emailTemplate.Remarks = "无";
                #endregion
                bool success = bussiness.Add(emailTemplate);
                if (success)
                {
                    return Json(new { Success = true, Message = "添加成功！" });
                }
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "邮件模板添加失败", "emailTemplate=" + JsonConvert.SerializeObject(emailTemplate), ex, "DapperCore");
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Edit(Sys_EmailTemplate emailTemplate)
        {
            try
            {
                Sys_EmailTemplateBusiness bussiness = new Sys_EmailTemplateBusiness();
                #region 完善Sys_EmailTemplate
                Sys_EmailTemplate old = bussiness.Get(emailTemplate.Id);
                old.TemplateCode = emailTemplate.TemplateCode;
                old.TemplateName = emailTemplate.TemplateName;
                old.TemplateTitle = emailTemplate.TemplateTitle;
                old.TemplateContent = emailTemplate.TemplateContent;
                old.Modifier = SessionUtility.GetBasicUserInfo().UserName;
                old.ModifyDate = DateTime.Now;
                #endregion
                bool success = bussiness.UpdateNotNull(old);
                if (success)
                {
                    return Json(new { Success = true, Message = "修改成功！" });
                }
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "邮件模板修改失败", "role=" + JsonConvert.SerializeObject(emailTemplate), ex, "DapperCore");
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Delete(string ids)
        {
            try
            {
                bool success = new Sys_EmailTemplateBusiness().Delete(ids, SessionUtility.GetBasicUserInfo().UserName);
                if (success)
                {
                    return Json(new { Success = true, Message = "删除成功！" });
                }
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "邮件模板删除失败", "ids=" + ids, ex, "DapperCore");
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
        }
    }
}